package com.android.yinghebike.ui.view

import android.content.Context
import android.util.AttributeSet
import android.view.View.OnClickListener
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.android.baselibrary.interface_.CommCallBack
import com.android.baselibrary.tool.Log
import com.android.baselibrary.util.Util
import com.android.baselibrary.view.GridSpacingItemDecoration
import com.android.yinghebike.R
import com.android.yinghebike.adapter.Nav_RouterListAdapter
import com.mapbox.api.directions.v5.models.DirectionsRoute
import com.mapbox.geojson.Point
import com.mapbox.search.ui_custom.adapter.engines.SearchEntityPresentation
import com.mapbox.search.ui_custom.utils.format.DistanceFormatter
import com.mapbox.search.ui_custom.utils.wrapWithSearchTheme
import com.mapbox.search.ui_custom.view.CommonSearchViewConfiguration
import com.mapbox.search.ui_custom.view.place.SearchPlace
import java.math.RoundingMode
import java.text.DecimalFormat

//底部控件
class Nav_SearchPlace_View @JvmOverloads constructor(
    outerContext: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
    defStyleRes: Int = 0,
) : FrameLayout(wrapWithSearchTheme(outerContext), attrs, defStyleAttr, defStyleRes),
    CoordinatorLayout.AttachedBehavior {

    var tv_address: TextView
    var tv_address1: TextView
    var ll_start: LinearLayout
    var ll_nav: LinearLayout
    lateinit var iv_close: ImageView
    lateinit var onBtnClick: OnBtnClick
    lateinit var point: Point
    lateinit var recyclerview: RecyclerView
    lateinit var adapter: Nav_RouterListAdapter

    private var distanceFormatter = DistanceFormatter(context)
    private var searchEntityPresentation: SearchEntityPresentation
    private var commonSearchViewConfiguration = CommonSearchViewConfiguration()

    init {
        searchEntityPresentation = SearchEntityPresentation(context)
        inflate(context, R.layout.view_nav_search_place, this)

        tv_address = findViewById(R.id.tv_address)
        tv_address1 = findViewById(R.id.tv_address1)
        ll_start = findViewById(R.id.ll_start)
        ll_nav = findViewById(R.id.ll_nav)
        iv_close = findViewById(R.id.iv_close)

        ll_start.setOnClickListener {
            if (onBtnClick != null) {
                onBtnClick!!.startClick(point)
            }
        }
        ll_nav.setOnClickListener(OnClickListener {
            if (onBtnClick != null) {
                onBtnClick.navClick(point)
            }
        })
        iv_close.setOnClickListener(OnClickListener {
            if (onBtnClick != null) {
                onBtnClick.closeClick()
            }
        })

        recyclerview = findViewById(R.id.recyclerview)
        recyclerview.layoutManager = GridLayoutManager(context, 3)
        recyclerview.addItemDecoration(
            GridSpacingItemDecoration(
                3,
                Util.dip2px(context, 10f),
                false
            )
        )
        adapter = Nav_RouterListAdapter(context, object : CommCallBack {
            override fun onResult(obj: Any?) {
                var route: DirectionsRoute = obj as DirectionsRoute;
                if (onBtnClick != null) {
                    onBtnClick.selectRouter(route)
                }
            }
        })
        recyclerview.adapter = adapter

    }


    fun setBtnClick(onBtnClick: OnBtnClick?) {
        this.onBtnClick = onBtnClick!!
    }

    interface OnBtnClick {
        fun navClick(point: Point?)
        fun startClick(point: Point?)
        fun closeClick()
        fun selectRouter(router: DirectionsRoute)

    }

    public fun setSearchPlace(searchPlace: SearchPlace) {
        this.point = searchPlace.coordinate
        tv_address.text = searchPlace.name
//        categoryText.setTextAndHideIfBlank(searchEntityPresentation.firstCategoryName(context, searchPlace.categories))
        tv_address1.text = searchEntityPresentation.getAddressOrResultType(searchPlace)
        tv_address1.visibility = VISIBLE
        recyclerview.visibility = GONE
    }

    public fun setSearchPoint(point: Point) {
        this.point = point
        tv_address.text = point.latitude().toString() + " ， " + point.longitude()
        tv_address1.visibility = GONE
        recyclerview.visibility = GONE
    }

    //路线列表
    public fun setRouter(routes: List<DirectionsRoute>) {
        for (route: DirectionsRoute in routes) {
            Log.i("列表：" + route.routeIndex())
            adapter.setData(routes)
        }
        adapter.setSelect(routes.first())
        recyclerview.visibility = VISIBLE
//        ll_nav.visibility = GONE
    }

    //选择路线
    public fun setRouter(route: DirectionsRoute?) {
        if (route !== null) {
            Log.i("选中：" + route.routeIndex())
            adapter.setSelect(route)
            //距离
//            ll_router_info.visibility = VISIBLE
            var distance = route.distance() * 0.621371 / 1000
//            tv_router_info.setText("" + getFloatNoMoreThanThreeDigits(distance) + "mi")

//            if (distance >= 1000) {
//                tv_router_info.setText("" + getFloatNoMoreThanTwoDigits(distance / 1000) + "mi")
//            } else {
//                tv_router_info.setText("" + getFloatNoMoreThanTwoDigits(distance) + "mi")
//            }
            //用时
//            tv_router_time.setText(""+  getFloatNoMoreThanOneDigits(route.duration()/60) +" min")
        } else {
//            ll_router_info.visibility = GONE
        }
    }

    fun getFloatNoMoreThanOneDigits(number: Double): String {
        val format = DecimalFormat("#.#")
        //舍弃规则，RoundingMode.FLOOR表示直接舍弃。
        format.roundingMode = RoundingMode.FLOOR
        return format.format(number)
    }

    fun getFloatNoMoreThanThreeDigits(number: Double): String {
        val format = DecimalFormat("#.###")
        //舍弃规则，RoundingMode.FLOOR表示直接舍弃。
        format.roundingMode = RoundingMode.FLOOR
        return format.format(number)
    }

    override fun getBehavior(): CoordinatorLayout.Behavior<*> {
        TODO("Not yet implemented")
    }
}